package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.StorageMapper;
import com.cskaoyan.model.Storage;
import com.cskaoyan.model.StorageExample;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.BaseRespVO;
import com.cskaoyan.model.vo.StorageListDataVO;
import com.cskaoyan.model.vo.StorageVO;
import com.cskaoyan.service.StorageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @program: mall38
 * @description: 对象存储
 * @author: Lexis
 * @create: 2022-05-05 20:51
 **/
@Service
@Transactional
public class StorageServiceImpl implements StorageService {
    @Autowired
    StorageMapper storageMapper;

    @Override
    public void create(StorageVO fileInfo) {
        storageMapper.insertOneFile(fileInfo);
    }

    @Override
    public BaseRespVO<StorageListDataVO> storgeList(BaseParamBO baseParamBO) {

        StorageExample storageExample = new StorageExample();
        storageExample.setOrderByClause(baseParamBO.getSort()+" "+baseParamBO.getOrder());
        StorageExample.Criteria criteria = storageExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if(baseParamBO.getKey()!=null&&baseParamBO.getKey()!=""){
            criteria.andKeyEqualTo(baseParamBO.getKey());
        }
        if(baseParamBO.getName()!=null&&baseParamBO.getKey()!=""){
            criteria.andNameLike("%"+baseParamBO.getName()+"%");
        }
        PageHelper.startPage(baseParamBO.getPage(),baseParamBO.getLimit());
        List<Storage> storages = storageMapper.selectByExample(storageExample);
        PageInfo<Storage> storagePageInfo = new PageInfo<>(storages);
        long total = storagePageInfo.getTotal();
        int pages = storagePageInfo.getPages();
        StorageListDataVO storageListDataVO = new StorageListDataVO();
        storageListDataVO.setLimit(baseParamBO.getLimit());
        storageListDataVO.setList(storages);
        storageListDataVO.setPage(baseParamBO.getPage());
        storageListDataVO.setPages(pages);
        storageListDataVO.setTotal((int)total);
        BaseRespVO<StorageListDataVO> storageListDataVOBaseRespVO = new BaseRespVO<>();
        storageListDataVOBaseRespVO.setData(storageListDataVO);
        storageListDataVOBaseRespVO.setErrmsg("成功");
        storageListDataVOBaseRespVO.setErrno(0);
        return storageListDataVOBaseRespVO;
    }

    @Override
    public BaseRespVO<Storage> storageUpdate(Storage storage) {
        storage.setUpdateTime(new Date(System.currentTimeMillis()));
        storageMapper.updateByPrimaryKeySelective(storage);
        BaseRespVO<Storage> storageBaseRespVO = new BaseRespVO<>();
        storageBaseRespVO.setErrno(0);
        storageBaseRespVO.setErrmsg("成功");
        storageBaseRespVO.setData(storage);
        return storageBaseRespVO;
    }

    @Override
    public BaseRespVO storgeDelete(Storage storage) {
        storage.setDeleted(true);
        storage.setUpdateTime(new Date(System.currentTimeMillis()));
        storageMapper.updateByPrimaryKeySelective(storage);
        BaseRespVO baseRespVO = new BaseRespVO();
        baseRespVO.setErrmsg("成功");
        baseRespVO.setErrno(0);
        return baseRespVO;
    }
}
